package com.example.easyscript.service.master.intf;

import com.example.easyscript.bean.StepHandler;
import com.example.easyscript.bean.task.StepBean;
import com.example.easyscript.bean.task.TaskBean;
import com.example.easyscript.utils.SpringContextHolder;

import java.util.Collection;
import java.util.Map;

/**
 * 步骤处理
 */
public interface StepHandlerService {
    /**
     * 获取步骤执行器
     *
     * @param stepBean 步骤
     * @return 获取步骤选择器
     */
    default public StepHandler getStepHandler(StepBean stepBean) {
        Map<String, StepHandler> beans = SpringContextHolder.getBeans(StepHandler.class);
        Collection<StepHandler> handlers = beans.values();
        for (StepHandler handler : handlers) {
            if (handler.canSolve(stepBean)) {
                return handler;
            }
        }
        throw new RuntimeException("找不到对应步骤的处理器");
    }

    /**
     * 处理步骤
     * @param stepBean 步骤
     * @return 返回执行的结果
     */
    default public boolean executionStep(StepBean stepBean, TaskBean taskBean) {
        StepHandler stepHandler = getStepHandler(stepBean);
        return stepHandler.executionStep(stepBean,taskBean);
    }
}
